package com.we823.cms.repository.service.beetl.tag;

import com.we823.cms.common.web.pageable.Direction;
import com.we823.cms.common.web.pageable.Sort;
import org.apache.commons.lang3.StringUtils;
import org.beetl.core.GeneralVarTagBinding;

public abstract class AbstractTag extends GeneralVarTagBinding {

	protected static final String PARAM_TAG = "tag";
	protected static final String PARAM_ORDERBY = "orderby";
	protected static final String PARAM_SORT = "sort";
	
	protected String outTag = PARAM_TAG;
	protected Sort sort = null;
	
	public void getParams() {
		outTag = getParam(PARAM_TAG,PARAM_TAG);
		String orderby = getParam(PARAM_ORDERBY,"orderid");
		String ascOrDesc = getParam(PARAM_SORT,"asc");
		
		if("asc".equals(ascOrDesc)){
			sort = new Sort(Direction.ASC,orderby);
		}else{
			sort = new Sort(Direction.DESC,orderby);
		}
	}
	

	@Override
	public void render() {
		getParams();
		execute();
		this.doBodyRender();
	}
	
	public abstract void execute();

	
	public Integer getParam(String param,Integer defaultValue) {
		String value = (String) getAttributeValue(param);
		if (StringUtils.isNumeric(value)) {
			return Integer.parseInt(value);
		}else {
			return defaultValue;
		}
	}
	
	public String getParam(String param) {
		return (String) getAttributeValue(param);
	}
	
	public String getParam(String param,String defaultValue) {
		String result = (String) getAttributeValue(param);
		if (StringUtils.isEmpty(result)) {
			return defaultValue;
		}else {
			return result;
		}
	}
}
